package main

import (
	"fmt"
	"net/http"
)

func main() {
	//curl -X GET -i  http://localhost:8989/get\?nam\=wpp\&nam\=ww11\&age\=2132
	//获取get的query参数
	http.HandleFunc("/get", func(writer http.ResponseWriter, request *http.Request) {
		fmt.Fprintf(writer, "Get query\n")
		handleMap(request.URL.Query(), writer)
	})

	//获取post的form参数
	//curl -X POST -i  --data "name=wpp&age=213" http://localhost:8989/post\?name\=ww1132
	http.HandleFunc("/post", func(writer http.ResponseWriter, request *http.Request) {
		fmt.Fprintf(writer, "Post form\n")
		request.ParseForm()
		handleMap(request.Form, writer)
	})

	//处理静态资源
	fileServer := http.FileServer(http.Dir("static/"))

	// 将 /static/ 的请求,全部交给fileSever处理,  处理时,需要去除路径的前缀,否则将会出现多一层static目录的问题
	http.Handle("/static/", http.StripPrefix("/static/", fileServer))
	//http.Handle("/static/", fileServer)

	http.ListenAndServe(":8989", nil)
}
func handleMap(values map[string][]string, writer http.ResponseWriter) {
	for k, vs := range values {
		s := ""
		for _, v := range vs {
			s += v
			s += ","
		}
		s = s[:len(s)-1]
		fmt.Fprintf(writer, "%s : %s \n", k, s)
	}
}
